import * as ipParamService from '../../services/resource/ipParam';
import { create } from 'domain';

export default {
  namespace: 'ipParam',
  state: {
    list: [],
    current : 1,
    currentSize : 10,
    total : 0, 
    modalVisible : false,
    modalType : 'create',
    newKey : '',
    currentItem: {},
    viewModalVisible:false,
  },
  reducers: {
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },   
    showModal (state, { payload }) {
      return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false, currentItem:{}, newKey : new Date().getTime() + '' }
    },
    showViewModal (state, { payload }) {
      return { ...state, ...payload, viewModalVisible: true }
    },   
    hideViewModal (state) {
      return { ...state, viewModalVisible: false, }
    },
   
    
  },
  effects: {   
    *queryList({ payload },{ call, put }) {
      const { body } = yield call(ipParamService.queryList,payload);
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },
    *qryById({ payload },{ call, put }){
      const { body } = yield call(ipParamService.qryById,payload);
      yield put({type: 'showModal', payload: { currentItem:body.data, modalType: 'edit',}});     
    },
    *create({ payload },{ call, put }){
      const { body } = yield call(ipParamService.create,payload.data);
      yield put({ type: 'queryList',payload:{ 'networkName':'' }});
      yield put({type: 'hideModal'});
    },
    *edit({ payload },{ call, put }){
      const { body } = yield call(ipParamService.edit,payload.data);
      yield put({ type: 'queryList', payload:payload.search });
      yield put({type: 'hideModal'});
    },
    *remove({ payload },{ call, put }){
      const { body } = yield call(ipParamService.remove,payload.id);
      yield put({ type: 'queryList', payload:payload.search });
    },
   
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/ipParam') {
          dispatch({ type: 'queryList', payload:{ 'networkName':'' }});
        }
      });
    },
  },
};
